<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="物料名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料编号" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入物料编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料子编号" prop="itemId">
        <el-input
          v-model="queryParams.itemId"
          placeholder="请输入物料子编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="spec">
        <el-input
          v-model="queryParams.spec"
          placeholder="请输入规格型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料分类" prop="classId">
        <el-select style="width: 100%" v-model="queryParams.classId" placeholder="物料分类" >
          <el-option
            v-for="dict in classOptions"
            :key="dict.classId"
            :label="dict.className"
            :value="dict.classId"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="供应商Id" prop="supplyId">-->
<!--        <el-input-->
<!--          disabled-->
<!--          v-model="queryParams.supplyId"-->
<!--          placeholder="请输入供应商Id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :max-height="tableHeight" :data="iteminfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="物料编号" width="140" show-overflow-tooltip align="center" prop="itemCode" />
      <el-table-column label="物料子编号" width="140" show-overflow-tooltip align="center" prop="itemId" />
      <el-table-column label="物料名称" width="140" show-overflow-tooltip align="center" prop="itemName" />
      <el-table-column label="物料单位" align="center" prop="unit">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.jc_iteminfo_unit" :value="scope.row.unit"/>
        </template>
      </el-table-column>
      <el-table-column label="物料图片" align="center" prop="url">
        <template slot-scope="scope">
          <image-preview :src="scope.row.url || ''" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="规格" align="center" prop="spec" />
      <el-table-column label="条码号" align="center" prop="tmh" />
      <el-table-column label="物料分类名称" align="center" prop="className" />
      <el-table-column label="物料大类名称" align="center" prop="classBigName" />
      <el-table-column label="供应商名称" align="center" prop="supplyName" />
      <el-table-column label="包装方式" align="center" prop="baozhuangType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.jc_iteminfo_bz" :value="scope.row.baozhuangType"/>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改物料信息对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">


        <el-form-item label="物料编号" prop="itemCode">
          <el-input v-model="form.itemCode" placeholder="请输入物料编码" />
        </el-form-item>

        <el-form-item label="物料名称" prop="itemName">
          <el-input v-model="form.itemName" placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="物料单位" prop="unit">
          <el-select v-model="form.unit" placeholder="请选择物料单位">
            <el-option
              v-for="dict in dict.type.jc_iteminfo_unit"
              :key="dict.value"
              :label="dict.label" :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="包装方式" prop="baozhuangType">
          <el-select v-model="form.baozhuangType" placeholder="请选择包装方式">
            <el-option
              v-for="dict in dict.type.jc_iteminfo_bz"
              :key="dict.value"
              :label="dict.label" :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="包装数量" prop="baozhuangQty">
          <el-input v-model="form.baozhuangQty" placeholder="请输入包装数量" />
        </el-form-item>
        <el-form-item label="规格" prop="spec">
          <el-input v-model="form.spec" placeholder="请输入规格" />
        </el-form-item>
        <el-form-item label="型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入型号" />
        </el-form-item>
        <el-form-item label="请选择大类" prop="classId">
              <el-select style="width: 100%" v-model="form.classId" placeholder="大类名称" >
                <el-option
                  v-for="dict in classOptions"
                  :key="dict.id"
                  :label="dict.className"
                  :value="dict.id"
                />
              </el-select>
            </el-form-item>

        <el-form-item label="请选择供应商" prop="supplyId">
          <el-select style="width: 100%" v-model="form.supplyId" placeholder="供应商" >
            <el-option
              v-for="dict in supplyOptions"
              :key="dict.id"
              :label="dict.supplyName"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="预警上线" prop="upLine">
          <el-input v-model="form.upLine" placeholder="请输入预警上线" />
        </el-form-item>
        <el-form-item label="预警下线" prop="downLine">
          <el-input v-model="form.downLine" placeholder="请输入预警下线" />
        </el-form-item>
        <el-form-item label="中间值" prop="middleLine">
          <el-input v-model="form.middleLine" placeholder="请输入中间值" />
        </el-form-item>
        <el-form-item label="停用标记" prop="stopFlag">
          <el-input v-model="form.stopFlag" placeholder="请输入停用标记" />
        </el-form-item>
        <el-form-item label="存储期" prop="cunchudate">
          <el-input v-model="form.cunchudate" placeholder="请输入存储期" />
        </el-form-item>
        <el-form-item label="保质期" prop="productiondate">
          <el-input v-model="form.productiondate" placeholder="请输入保质期" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listIteminfo, getIteminfo, delIteminfo, addIteminfo, updateIteminfo } from "@/api/base/iteminfo";
import { listClass} from "@/api/base/class";
import { listSupplier} from "@/api/supplychain/supplier";

export default {
  name: "IteminfoSelect",
  dicts: ['jc_iteminfo_unit', 'jc_iteminfo_bz'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料信息表格数据
      iteminfoList: [],
      // 表格的高度
      tableHeight: document.documentElement.scrollHeight - 245 + "px",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //大类单位字典
      classOptions: [],
      //供应商字典
      supplyOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        itemName: null,
        itemCode: null,
        unit: null,
        baozhuangType: null,
        baozhuangQty: null,
        spec: null,
        model: null,
        tmh: null,
        classId: null,
        className: null,
        supplyId: null,
        supplyName: null,
        upLine: null,
        downLine: null,
        middleLine: null,
        stopFlag: null,
        cunchudate: null,
        productiondate: null,
        codesParams:[]

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deptId: [
          { required: true, message: "部门id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    //this.getList();
    listClass().then(response => {
    this.classOptions = response.rows;
      });
    listSupplier().then(response => {
      this.supplyOptions = response.rows;
    });

  },
  methods: {
    /** 查询物料信息列表 */
    getList() {
      this.loading = true;
      listIteminfo(this.queryParams).then(response => {
        this.iteminfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getItemInfoList(codesParams,classId){
      this.queryParams.codesParams=codesParams;
      this.queryParams.classId=classId;
      this.getList();
    },

    //大类翻译
    classFormat(row, column) {
      var actions = [];
      Object.keys(this.classOptions).some((key) => {
        if (this.classOptions[key].id == ('' + row.classId)) {
          actions.push(this.classOptions[key].className);
          return true;
        }
      })
      return actions.join('');
    },


    //供应商翻译
    supplyFormat(row, column) {
      var actions = [];
      Object.keys(this.supplyOptions).some((key) => {
        if (this.supplyOptions[key].id == ('' + row.supplyId)) {
          actions.push(this.supplyOptions[key].supplyName);
          return true;
        }
      })
      return actions.join('');
    },


    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        itemName: null,
        unit: null,
        baozhuangType: null,
        baozhuangQty: null,
        spec: null,
        model: null,
        tmh: null,
        classId: null,
        className: null,
        supplyId: null,
        supplyName: null,
        upLine: null,
        downLine: null,
        middleLine: null,
        stopFlag: null,
        cunchudate: null,
        productiondate: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物料信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getIteminfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物料信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateIteminfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addIteminfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除物料信息编号为"' + ids + '"的数据项？').then(function() {
        return delIteminfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('base/iteminfo/export', {
        ...this.queryParams
      }, `iteminfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
